package com.hnkywl.sts.action.flow.assign;

import com.hnkywl.sts.service.sys.YgService;
import org.jbpm.api.ExecutionService;
import org.jbpm.api.TaskService;
import org.jbpm.api.cmd.Environment;
import org.jbpm.api.model.Transition;
import org.jbpm.pvm.internal.cmd.AbstractCommand;
import org.jbpm.pvm.internal.model.ActivityImpl;
import org.jbpm.pvm.internal.model.ExecutionImpl;
import org.jbpm.pvm.internal.model.TransitionImpl;
import org.jbpm.pvm.internal.task.TaskImpl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: jerry
 * Date: 13-4-25
 * Time: 下午2:24
 * To change this template use File | Settings | File Templates.
 */
public class RegectTaskSingleBackWayCmd extends AbstractCommand<String> {
    private String taskId;

    private String content;

    public RegectTaskSingleBackWayCmd(String taskId, String content) {
        super();
        this.taskId = taskId;
        this.content = content;
    }

    @Override
    public String execute(Environment env) throws Exception {

        TaskService taskService = env.get(TaskService.class);
        ExecutionService executionService = env.get(ExecutionService.class);
        TaskImpl currentTask = (TaskImpl) taskService.getTask(taskId);
        ExecutionImpl exec = currentTask.getProcessInstance();
        //获取当前任务的活动节点
        for (String activityName : exec.findActiveActivityNames()) {
            ActivityImpl currentActivity = exec.getProcessDefinition().findActivity(activityName);
            //获取该活动节点的IncomingTransition
            List<? extends Transition> incomingTransitions = currentActivity.getIncomingTransitions();
            TransitionImpl incomingTransition = null;
            if (incomingTransitions.size() > 1) {
                for (Transition incomingTransition1 : incomingTransitions) {
                    if (!incomingTransition1.getSource().getName().equals("判断")) {
                        incomingTransition = (TransitionImpl) incomingTransition1;
                        break;
                    }
                }
            } else {
                if (incomingTransitions.get(0).getSource().getName().equals("判断")) {
                    incomingTransition = (TransitionImpl) incomingTransitions.get(0).getSource()
                            .getIncomingTransitions().get(0);
                } else {
                    incomingTransition = (TransitionImpl) incomingTransitions.get(0);
                }
            }
            //获取IncomingTransition的源活动节点
            ActivityImpl sourceActivity = incomingTransition.getSource();
            if (sourceActivity.getType().equals("fork")) {
                Map<String, Object> var = new HashMap<String, Object>();
                var.put("owner", YgService.getCurrentYg().getId());
                var.put("opera", YgService.getCurrentYgGs() + "--" + YgService.getCurrentYgName());
                var.put("result", "驳回");
                var.put("content", content);
                var.put("time", new Date());
                executionService.createVariables(currentTask.getExecutionId(), var, true);
//                taskService.setVariables(taskId, var);
                //executionService.signalExecutionById(currentTask.getExecutionId(),var);
                var.put("status", "驳回");
                taskService.completeTask(taskId, var);
                if(currentActivity.getOutgoingTransitions().get(0).getDestination().getName().equals("结合")){
                    return "change";
                }
            } else {
                TransitionImpl backTransition = currentActivity.createOutgoingTransition();
                //当前活动节点为新的回退Transition的源，而原来的“源”活动节点变成了目标活动节点
                backTransition.setSource(currentActivity);
                backTransition.setDestination(sourceActivity);
                backTransition.setName("退回" + activityName);
                Map<String, Object> var = new HashMap<String, Object>();
                var.put("owner", YgService.getCurrentYg().getId());
                var.put("opera", YgService.getCurrentYgGs() + "--" + YgService.getCurrentYgName());
                var.put("result", "驳回");
                var.put("content", content);
                var.put("time", new Date());
                executionService.createVariables(currentTask.getExecutionId(), var, true);
                var.put("status", "驳回");
                taskService.completeTask(taskId, backTransition.getName(), var);

                if (sourceActivity.getIncomingTransitions().get(0).getSource().getName().equals("开始")) {
                    return "change";
                }
            }
            break;
        }
        return "complete";
    }
}
